/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditEmployee.java
 *
 * Created on Jan 7, 2011, 6:55:51 PM
 */

package Graphic.DataManager.Employee;

import Client.RequestI;
import Data.Employee;
import XMLPack.Save2Csv;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Cristina
 */
public class EditEmployee extends javax.swing.JPanel {

    /** Creates new form EditEmployee */
    private RequestI request = null;
    public EditEmployee(RequestI r)
    {
        request = r;
        initComponents();
        model = (DefaultTableModel)employeeTable.getModel();
        employeeTable.setModel(model);
    }
     public void Show()
    {

         while(model.getRowCount()>0)
         model.removeRow(0);
         
        String str = request.employeeSalary("dmstc");
        request.getClient().SetRequest(str);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        employeeTable = new javax.swing.JTable();
        updateBtn = new javax.swing.JButton();
        DeleteBtn = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        employeeTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "First Name", "Last Name", "Earning", "Work Hours", "Bonus"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, true, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(employeeTable);

        updateBtn.setText("Update Table");
        updateBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateBtnActionPerformed(evt);
            }
        });

        DeleteBtn.setText("Delete");
        DeleteBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteBtnActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setText("Employees");

        jButton1.setText("Save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addComponent(DeleteBtn)
                            .addGap(55, 55, 55)
                            .addComponent(updateBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton1))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 348, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(38, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(9, 9, 9)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DeleteBtn)
                    .addComponent(jButton1)
                    .addComponent(updateBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void updateBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateBtnActionPerformed


        ArrayList<Employee> employeeList = new ArrayList();
         for(int i = 0; i<model.getRowCount(); i++)
         {
          System.out.println("updateBtn "+i);
            Employee e = new Employee(
                   (String) model.getValueAt(i,0),
                   (String) model.getValueAt(i, 1),
                   (Integer)model.getValueAt(i, 2),
                   (Integer)model.getValueAt(i, 3),
                   (Integer)model.getValueAt(i, 4));
                  
            employeeList.add(e);

         }
        System.out.println("*******UpdateTable**************");
        String str = request.editEmployees("dmstc", employeeList);
        request.getClient().SetRequest(str);
        //Show();
    }//GEN-LAST:event_updateBtnActionPerformed

    private void DeleteBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteBtnActionPerformed
       if(model.getRowCount() >0 && (employeeTable.getSelectedRow() > -1)){

           System.out.println("*************in DELETE BUTTON *********");
         System.out.println((String)model.getValueAt(employeeTable.getSelectedRow(), 0)+" "
                 +(String)model.getValueAt(employeeTable.getSelectedRow(), 1));
         System.out.println("**********************");
         String filename = request.deleteEmployee("dmstc",
                 (String)model.getValueAt(employeeTable.getSelectedRow(), 0),
                 (String)model.getValueAt(employeeTable.getSelectedRow(), 1));
         
         request.getClient().SetRequest(filename);
           /* try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
                Logger.getLogger(EditEmployee.class.getName()).log(Level.SEVERE, null, ex);
            }*/
         //Show();
        }
    }//GEN-LAST:event_DeleteBtnActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
         String wd = System.getProperty("user.dir");
       JFileChooser fc = new JFileChooser(wd);
    int rc = fc.showDialog(null, "Select Data File");
    if (rc == JFileChooser.APPROVE_OPTION)
    {
    File file = fc.getSelectedFile();
    String filename = file.getAbsolutePath();
    Save2Csv saveObj = new Save2Csv();
    saveObj.Save(employeeTable, filename);
    }
    else
    System.out.println("File chooser cancel button clicked");
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton DeleteBtn;
    private javax.swing.JTable employeeTable;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton updateBtn;
    // End of variables declaration//GEN-END:variables
    private DefaultTableModel model;
    public DefaultTableModel getModel(){return model;}
}
